package traverser;

import java.util.Arrays;
import java.util.HashMap;

public class KeyTracker {
	private String endLink;
	private HashMap<String, Integer> keys;
	private CacheLinkProcessor p;
	private String[] targetKeyword;
	
	private boolean new_ranks = true;

	public KeyTracker(String url, boolean new_ranks) {
		endLink = url;
		splitTarget();
		fillHashMap();
		this.new_ranks = new_ranks;
	}

	public void splitTarget() {
		String tempEndLink = endLink;
		tempEndLink = tempEndLink.substring(29);
		tempEndLink = tempEndLink.replace("of_", "");
		tempEndLink = tempEndLink.replace("the_", "");
		tempEndLink = tempEndLink.replace("think", "");
		tempEndLink = tempEndLink.replace("and_", "");
		tempEndLink = tempEndLink.replace("with_", "");
		tempEndLink = tempEndLink.replace("or_", "");
		tempEndLink = tempEndLink.replace("(", "");
		tempEndLink = tempEndLink.replace(")", "");
		// tempEndLink = tempEndLink.toLowerCase();
		targetKeyword = tempEndLink.split("_");

		/*
		 * for (String str : targetKeyword) { System.out.println(str); }
		 */

		// TODO: rather than use string.replace() (which uses regex and so will
		// be slow) it's
		// probably better to split it first and check each of the substrings
		// st your
		// stop words.
	}

	public static String[] splitAndReduce(String link) {
		link = link.substring(29);
		link = link.toLowerCase();
		link = link.replace("(", "");
		link = link.replace(")", "");
		link = link.replace(":", "");
		link = link.replace(",", "");
		link = link.replace(".", "");
		link = link.replace("!", "");
		link = link.replace("_and_", "_");
		link = link.replace("?", "");
		link = link.replace("_to_", "_");
		link = link.replace("_me_", "_");
		link = link.replace("_in_", "_");
		link = link.replace("_of_", "_");
		link = link.replace("_at_", "_");
		link = link.replace("_from_", "_");
		link = link.replace("_by_", "_");
		link = link.replace("_on_", "_");
		link = link.replace("_the_", "_");
		link = link.replace("_a_", "_");
		link = link.replaceAll("_[a-z]_", "_");
		String[] results = link.split("_");
		int end = results.length - 1;

		// Remove stopwords (the, and, etc) from the list

		String[] temp = new String[end + 1];
		for (int i = 0; i < temp.length; i++) {
			temp[i] = results[i];
		}
		results = temp;
		return results;

	}

	public void fillHashMap() {
		String[] tempLink;
		keys = new HashMap<String, Integer>();
		p = new CacheLinkProcessor(this.endLink);
		tempLink = p.getLinks();
		String[][] keyWord = new String[tempLink.length][];
		int size = 0;

		for (int i = 0; i < tempLink.length; i++) {
			keyWord[i] = splitAndReduce(tempLink[i]);
			size += keyWord[i].length;

			// TODO: Same as above - split and then replace
		}

		String[] keyword_temp = new String[size];
		int z = 0;

		/*
		 * for(int i=0;i<keyWord.length;i++){ System.out.println(""); for(int
		 * j=0;j<keyWord[i].length;j++){ System.out.print(keyWord[i][j]+"  "); }
		 * }
		 */

		for (int i = 0; i < keyWord.length; i++) {
			for (int j = 0; j < keyWord[i].length; j++) {
				boolean b = false;
				for (int k = 0; k < z; k++) {
					if (keyword_temp[k].equals(keyWord[i][j])) {
						b = true;
					}
				}
				if (b == false) {
					keyword_temp[z] = keyWord[i][j];
					z++;
				}
			}
		}

		String[] keyword = new String[z];

		for (int i = 0; i < z; i++) {
			keyword[i] = keyword_temp[i];
		}

		// TODO: Note that we have variables keyword and keyWord - this is
		// TERRIBLE
		// coding style, for what should be obvious reasons.
		// Note: z == keyword.length

		int[] counter = new int[z];
		for (int i = 0; i < keyword.length; i++) {
			counter[i] = 0;
			for (int j = 0; j < keyWord.length; j++) {
				for (int k = 0; k < keyWord[j].length; k++) {
					if (keyword[i].equals(keyWord[j][k])) {
						counter[i]++;
					}
				}
			}
		}
		

		/*
		 * Edit by Dong to solve homonyms. I rewrite the counter, not by the
		 * frequency, reduce the difference between different keywords. And hope
		 * it can help reduce the effect brought by the problem of homonyms. The
		 * former program is below, we can compare them by data 1/04/2012
		 */
		
		if (new_ranks) {
			int[] a = new int[10000];
			for (int i = 0; i < z; i++) {
				a[counter[i]] = 1;
			}
			int sum = 0;
			HashMap<String, Integer> sequence = new HashMap();
			for (int i = 0; i < 10000; i++) {
				if (a[i] != 0) {
					sum++;
					sequence.put(Integer.toString(i), sum);
				}
			}
			for (int i = 0; i < keyword.length; i++) {
				if (Symbol(keyword[i]))
					keys.put(keyword[i], sum);
				else
					keys.put(keyword[i], sequence.get(Integer.toString(counter[i])));
			}
			
		} else {

			// This is the former program: It has large difference between different
			// keywords.
			for(int k=0;k<keyword.length;k++){
				keys.put(keyword[k], counter[k]);
			}
		}


		for (int k = 0; k < keyword.length; k++) {
			/*
			System.out.print(keyword[k]);
			int miku = keys.get(keyword[k]);
			System.out.print(" " + miku);
			System.out.println();
			*/
		}


		// System.out.println("The number of keywords is: "+keyword.length);
		// System.out.println("keyword: "+keyword[i]+" :priority "+returnHashmap(keyword[i]));
	}

	// This method is used to test whether the keyword is a year. I just take
	// those year consists of 4 digits into account. Because they are the most
	// frequent ones.
	// And why year some happens is a very important keyword for us to traverse
	// in the wikipedia.
	private boolean Symbol(String s) {
		boolean b = true;
		if (s.length() == 4) {
			char char1 = s.charAt(0);
			char char2 = s.charAt(1);
			char char3 = s.charAt(2);
			char char4 = s.charAt(3);
			int n1 = (int) char1;
			int n2 = (int) char2;
			int n3 = (int) char3;
			int n4 = (int) char4;
			if ((n1 >= 48 && n1 <= 57) && (n2 >= 48 && n2 <= 57)
					&& (n3 >= 48 && n3 <= 57) && (n4 >= 48 && n4 <= 57))
				return true;
		}
		if (s.contains("-"))
			return true;
		return false;
	}

	public int priorityOf(String word) {
		Object o = keys.get(word);
		if (o == null)
			return 0;
		else
			return keys.get(word);
	}

	public static void main(String[] args) {
		String url = "http://en.wikipedia.org/wiki/Photograph";
		KeyTracker ki = new KeyTracker(url, true);
		// System.out.print(ki.priorityOf("autonomous"));
	}

}